
from drf_yasg2 import openapi
from drf_yasg2.utils import swagger_auto_schema
from rest_framework import status, views
from rest_framework.decorators import action
from rest_framework.response import Response

from PAA_ERP.utils.cont import NumberPrefix
from PAA_ERP.utils.generate_code import generate_code


class MultipleDestroyMixin:
    '''
    批量删除的视图函数
    '''
    del_ids = openapi.Schema(type=openapi.TYPE_OBJECT, required=['ids'], properties={
        'ids': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_INTEGER),
                              description='选择那些需要删除的id（主键）列表')
    })

    @swagger_auto_schema(method='delete', request_body=del_ids, operation_description='批量删除')
    @action(methods=['delete'], detail=False)
    def multiple_delete(self, request, *args, **kwargs):
        delete_ids = request.data.get('ids')
        if not delete_ids:
            return Response(data={'detail': '参数错误，ids为必传参数'}, status=status.HTTP_400_BAD_REQUEST)
        if not isinstance(delete_ids, list):
            return Response(data={'detail': '参数错误，ids参数必须是一个列表'}, status=status.HTTP_400_BAD_REQUEST)
        queryset = self.get_queryset()
        del_queryset = queryset.filter(id__in=delete_ids)
        if del_queryset.count() != len(delete_ids):
            return Response(data={'detail': '删除数据不存在'}, status=status.HTTP_400_BAD_REQUEST)
        del_queryset.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
        0


class MultipleOpenMixin:
    '''
    批量启用或禁用的视图函数
    '''
    del_ids = openapi.Schema(type=openapi.TYPE_OBJECT, required=['ids'], properties={
        'ids': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_INTEGER),
                              description='选择那些需要批量的id（主键）列表'),
        'is_open': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_INTEGER),
                                  description='是否启用，启用用0表示，禁用是1'),
    })

    @swagger_auto_schema(method='delete', request_body=del_ids, operation_description='批量请用或者禁用')
    @action(methods=['delete'], detail=False)
    def multiple_open(self, request, *args, **kwargs):
        delete_ids = request.data.get('ids')
        is_open = request.data.get('is_open', '0')
        if not delete_ids:
            return Response(data={'detail': '参数错误，ids为必传参数'}, status=status.HTTP_400_BAD_REQUEST)
        if not isinstance(delete_ids, list):
            return Response(data={'detail': '参数错误，ids参数必须是一个列表'}, status=status.HTTP_400_BAD_REQUEST)
        queryset = self.get_queryset()
        del_queryset = queryset.filter(id__in=delete_ids)

        if del_queryset.count() != len(delete_ids):
            return Response(data={'detail': '数据不存在'}, status=status.HTTP_400_BAD_REQUEST)

        del_queryset.updata(delete_flag=is_open)
        return Response(status=status.HTTP_200_OK)


# 生成各种编号的接口
class GenerateCode(views.APIView):
    prefix_param = openapi.Parameter(name='prefix', in_=openapi.IN_QUERY, description='编号前缀，可以参考cont.py文件',
                                     type=openapi.TYPE_STRING)

    @swagger_auto_schema(manual_parameters=[prefix_param], operation_description='自动生成各种流水号')
    def get(self, request, *args, **kwargs):
        """
        生成各种编号的接口，必须传一个前缀: /api/generate_code/prefix=ord，可以参考cont.py

        返回一个28位的编号字符串, return： code就是生成的编号
        """
        prefix = request.query_params.get('prefix', None)
        if prefix:
            if prefix in NumberPrefix.__members__:
                code = generate_code(NumberPrefix[prefix].value)
                return Response(data={'code': code}, status=status.HTTP_200_OK)
            return Response(data={'detail': 'prefix没有配置，参考cont.py'}, status=status.HTTP_400_BAD_REQUEST)
        return Response(data={'detail': 'prefix没有，该参数必须传'}, status=status.HTTP_400_BAD_REQUEST)
